package com.example.xm.ui.own.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xm.R;
import com.example.xm.base.BaseActivity;
import com.example.xm.bean.OwnBean.UserBean;
import com.example.xm.interfaces.own.IOwn;
import com.example.xm.presenter.own.LoginPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterActivity extends BaseActivity<IOwn.LoginPersenter> implements IOwn.LoginView {

    @BindView(R.id.toolbar_iv_back)
    ImageView toolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView toolbarTvTitle;
    @BindView(R.id.toolbar_iv_dian)
    ImageView toolbarIvDian;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pwd1)
    EditText etPwd1;
    @BindView(R.id.ll_register)
    LinearLayout llRegister;
    @BindView(R.id.btn_register)
    Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    @Override
    protected IOwn.LoginPersenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initListener() {
        toolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etName.getText().toString();
                String pwd = etPwd.getText().toString();
                String pwd1 = etPwd1.getText().toString();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd) && !TextUtils.isEmpty(pwd1)) {
                    if (pwd.equals(pwd1)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("username", name);
                        map.put("password", pwd);
                        mPresenter.register(map);
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        intent.putExtra("name", name);
                        intent.putExtra("pwd", pwd);
                        startActivity(intent);
                    } else {
                        Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RegisterActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void initView() {
        toolbarTvTitle.setText("注册");
        toolbarIvDian.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void loginReturn(UserBean userBean) {

    }

    @Override
    public void registerReturn(UserBean userBean) {
        if (userBean.getErr() == 200) {
            Toast.makeText(this, "注册成功,请登录", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, userBean.getErrmsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }
}